/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;

// JSP Name Space
class NameSpace {
   private LinkedList< Map<String, JavaType> > nameSpace        = new LinkedList< Map<String, JavaType> >();
   // TODO: W przyszlosci potrzebny jest jeszcze typeSpace, uwzgledniajacy efekty dyrektywy import...

   NameSpace() {
      pushContext();
      JavaType.init_name_space(this);
   }

   void pushContext() {
      nameSpace.addFirst(new LinkedHashMap<String, JavaType>());
   }

   void popContext() {
      nameSpace.removeFirst();
   }

   void declare(String name, JavaType type) {
      nameSpace.peek().put(name, type);
   }

   JavaType type(String name) throws TranslatorException {
      for(Map<String, JavaType> context: nameSpace) {
         if(context.containsKey(name)) {
            return context.get(name);
         }
      }

      return null;
   }

   public String toString() {
      StringBuilder sb=new StringBuilder();
      for(Map<String, JavaType> context: nameSpace) {
         sb.append("[ ");
         boolean first=true;
         for(String key: context.keySet()) {
            if(first) {
               first=false;
            }
            else {
               sb.append(",");
            }
            sb.append(key).append(":").append(context.get(key));
         }
         sb.append(" ]");
      }
      return sb.toString();
   }

}
